//
//  GYVerificationViewController.m
//  GoodLuck
//
//  Created by apple on 2018/6/23.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

#import "GYVerificationViewController.h"
#import "GYNetWorkTools.h"
#import "GYSetPayPwdViewController.h"

@interface GYVerificationViewController ()

@property (weak, nonatomic) IBOutlet UITextField *phoneTF;
@property (weak, nonatomic) IBOutlet UITextField *verTF;
@property (weak, nonatomic) IBOutlet UIButton *verBtu;
@property (weak, nonatomic) IBOutlet UIButton *netBtu;

/** 定时器 */
@property (nonatomic, strong) dispatch_source_t timer;
@end

@implementation GYVerificationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
}

- (void)setupUI {
    self.navigationItem.title = @"验证身份";
    self.phoneTF.text = self.currentPhone;
    self.verBtu.layer.cornerRadius = 4;
    self.netBtu.layer.cornerRadius = 4;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

- (IBAction)onclick:(UIButton *)sender {
    switch (sender.tag ) {
        case 1:
            {
                NSLog(@"获取验证码...");
                if ([self isMobileNumber:self.phoneTF.text]) {
                    //1.发送验证码请求
                    [GYNetWorkTools requestPayPwdGetVerCode:^(NSDictionary *obj) {
                        NSLog(@"获取验证码=====obj===%@",obj);
                        int code = [[obj objectForKey:@"code"] intValue];
                        if (code == 200) {
                            //发送验证码
                            __block int count = 60;
                            sender.enabled = NO;
                            dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
                            
                            self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
                            dispatch_source_set_timer(self.timer, DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
                            dispatch_source_set_event_handler(self.timer, ^{
                                //2.2改变按钮文字
                                count = count - 1;
                                if (count <= 0) {
                                    self.timer = nil;
                                    dispatch_async(dispatch_get_main_queue(), ^{
                                        [sender setTitle:@"获取验证码" forState:UIControlStateNormal];
                                        sender.enabled = YES;
                                    });
                                }else {
                                    dispatch_async(dispatch_get_main_queue(), ^{
                                        [sender setTitle:[NSString stringWithFormat:@"%d秒",count] forState:UIControlStateNormal];
                                    });
                                }
                            });
                            dispatch_resume(self.timer);
                        }else if (code == 400) {
                            [EasyTextView showText:@"获取验证码失败,请重新获取!"];
                        }
                    } error:^(NSError *error) {
                        [EasyTextView showText:@"获取验证码失败,请重新获取!"];
                        NSLog(@"error=====%@",error);
                    }];
                }else {
                    [EasyTextView showText:@"请填写正确的手机号!"];
                }
            }
            break;
            
        case 2:
        {
            if (![self isMobileNumber:self.phoneTF.text]) {
                [EasyTextView showText:@"请填写正确的手机号!"];
                return;
            }

            if (self.verTF.text.length == 0) {
                [EasyTextView showText:@"请填写验证码!"];
                return;
            }

            [GYNetWorkTools requestVerPayPwdVerCode:self.verTF.text success:^(NSDictionary *obj) {
                //NSLog(@"obj========%@",obj);
                int code = [[obj objectForKey:@"code"] intValue];
                if (code == 200) {
                    //跳转设置支付密码界面
                    GYSetPayPwdViewController *setPayPwdVC = [[GYSetPayPwdViewController alloc] initWithNibName:@"GYSetPayPwdViewController" bundle:nil];
                    [self.navigationController pushViewController:setPayPwdVC animated:YES];
                }else if (code == 400) {
                    [EasyTextView showText:@"验证身份失败!"];
                }
            } error:^(NSError *error) {
                [EasyTextView showText:@"验证身份失败!"];
                NSLog(@"error=======%@",error);
            }];
        }
            break;
    }
}

#pragma mark--判断手机号码
- (BOOL)isMobileNumber:(NSString *)mobileNum
{
    //    电信号段:133/153/180/181/189/177
    //    联通号段:130/131/132/155/156/185/186/145/176
    //    移动号段:134/135/136/137/138/139/150/151/152/157/158/159/182/183/184/187/188/147/178
    //    虚拟运营商:170
    NSString * MOBILE = @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";
    /**
     10         * 中国移动：China Mobile
     11         * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     12         */
    //     NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";
    NSString * CM = @" ^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(18[2-4,7-8]))\\d{8}|(1705)\\d{7}$";
    /**
     15         * 中国联通：China Unicom
     16         * 130,131,132,152,155,156,185,186
     17         */
    //     NSString * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";
    NSString * CU =  @"^((13[0-2])|(145)|(15[5-6])|(176)|(18[5,6]))\\d{8}|(1709)\\d{7}$";
    /**
     20         * 中国电信：China Telecom
     21         * 133,1349,153,180,189
     22         */
    //     NSString * CT = @"^1((33|53|8[09])[0-9]|349)\\d{7}$";
    NSString * CT = @"^((133)|(153)|(177)|(18[0,1,9]))\\d{8}$";
    /**
     25         * 大陆地区固话及小灵通
     26         * 区号：010,020,021,022,023,024,025,027,028,029
     27         * 号码：七位或八位
     28         */
    // NSString * PHS = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";
    
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
    NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
    NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
    
    if (([regextestmobile evaluateWithObject:mobileNum] == YES)
        || ([regextestcm evaluateWithObject:mobileNum] == YES)
        || ([regextestct evaluateWithObject:mobileNum] == YES)
        || ([regextestcu evaluateWithObject:mobileNum] == YES))
    {
        return YES;
    }
    else
    {
        return NO;
    }
}

@end
